#!/usr/bin/perl 
use strict;
use warnings;

use Bio::SeqIO;

my $file;


# get the cmd line arguments
# 1st argument will be name of the input file

#warn("cmdline arguments were ",join(",",@ARGV),"\n");

# look into Getopt::Long for info on parsing cmdline arguments
# which are name (i.e. -in inputputfile -out outputfile)
$file = $ARGV[0];

# want to read in the sequence file and accomplish a few things
# 1. Total number of sequences
# 2. Total length of sequences
# 3. Number of sequences that are on Chromosome III

my ($sequence_count, $sequence_length,
    %seqs_by_chromosome);

# let's parse the file
# to parse a sequence file in Perl we use Bio::SeqIO

# open a file for READING
my $input = Bio::SeqIO->new( -format => 'fasta',
			     -file   => $file);

# these are examples of how we would open a file for WRITING
#my $output = Bio::SeqIO->new( -format => 'fasta',
#			      -file   => ">$file.output");
# example of standard perl way of opening file for reading
#open(FH, $file) || die; # input
# or for writing
#open(FH, ">$file") || die ; #output

# we would parse the file here
while( my $seq = $input->next_seq ) {
# seq is a seq object -- in  this case Bio::Seq object
#    warn("seq is $seq which is a ", ref($seq), "\n");
    $sequence_count++; # count the num of sequences, update this counter

    my $length = $seq->length; # get the sequence length
    $sequence_length += $length; # concatenate the length
    
    # remember in the yeast file the FASTA header looks like this
    # >YAL065C gene=YAL065C.gene chrI:complement(11566..11952)
    # 'YAL065C' is stored in $seq->display_id (aliased as $seq->id)
    # 'gene=YAL065C.gene chrI:complement(11566..11952)' is in $seq->description
    my $id = $seq->display_id;
    my $desc = $seq->description;
# some list stuff
#    my @list = split(/\s+/,$desc);
#    my $list_len = @list;
#    my $list_len = scalar @list;
#    my ($firstlist_item) = @list;   
#    print "list length is ",$list_len, "\n";
    
    my ($genename,$location) = split(/\s+/,$desc);
    my ($chr) = split(/:/,$location);
    $seqs_by_chromosome{$chr}++;
}


# this is the end

print $sequence_count, " sequences\n";
print $sequence_length," total length\n";
print $seqs_by_chromosome{'chrIII'}, " sequences on chromosome III\n";





